package cn.eng.basic.controller;

import cn.eng.basic.exception.BusinessException;
import cn.eng.basic.service.IVerifyCodeService;
import cn.eng.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 接口:验证码
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
     * 接口: 图形验证码
     * @param key
     * @return
     */
    @GetMapping("/image/{key}")
    public String getImageCode(@PathVariable("key") String key){
        return verifyCodeService.getImageCode(key);
    }

    /**
     * 接口: 注册获取手机验证码
     * @param map
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult getSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.getSmsCode(map);
            return new AjaxResult();
         } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
         e.printStackTrace();
         return new AjaxResult(false,"系统繁忙,请稍后再试");
         }
    }
    /**
     * 接口: 扫码登录时,绑定微信获取手机验证码
     * @param map
     * @return
     */
    @PostMapping("/binderSmsCode")
    public AjaxResult binderSmsCode(@RequestBody Map<String,String> map){
        try {
            verifyCodeService.binderSmsCode(map);
            return new AjaxResult();
        } catch (BusinessException e) {
            return new AjaxResult(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙,请稍后再试");
        }
    }
}
